package chd.edu.train.member.controller;

import chd.edu.train.common.resp.CommonResp;
import chd.edu.train.member.req.MemberLoginReq;
import chd.edu.train.member.req.MemberRegisterReq;
import chd.edu.train.member.req.MemberSendCodeReq;
import chd.edu.train.member.resp.MemberLoginResp;
import chd.edu.train.member.service.MemberService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * @author Zhang Chaoliang
 * create 2023
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    @GetMapping("/count")
    public CommonResp<Integer> count(){
        int count = memberService.count();
        CommonResp<Integer> commonResp = new CommonResp<>();
        commonResp.setContent(count);
        return commonResp;
    }

    /**
     * 注册
     * @param req
     * @return
     */
    @PostMapping("/register")
    public CommonResp<Long> register(@Valid MemberRegisterReq req){
        long register = memberService.register(req);

//        CommonResp<Long> commonResp = new CommonResp<>();
//        commonResp.setContent(register);
//        return commonResp;
        return new CommonResp<>(register);
    }

    /**
     * 会员未注册则进行注册并且发送短信验证码
     * @param req
     * @return
     */
    @PostMapping("/send-code")
    public CommonResp<Long> sendCode(@Valid @RequestBody MemberSendCodeReq req){
        memberService.sendCode(req);

        return new CommonResp<>();
    }

    /**
     * 登登录过程中发现未注册，提示用户先获取验证码，验证验证码是否正确
     * @param req
     * @return MemberLoginResp
     */
    @PostMapping("/login")
    public CommonResp<MemberLoginResp> login(@Valid @RequestBody MemberLoginReq req){
        MemberLoginResp resp = memberService.login(req);

        return new CommonResp<>(resp);
    }
}
